package com.example.sys.controller;

import com.example.sys.model.SysUser;
import com.example.sys.service.SysUserService;
import com.example.utils.ImageCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping("/sys")
@Slf4j
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public String login(){

        return "/login";
    }

    @GetMapping("/code")
    @ResponseBody
    public void imageCode(HttpServletRequest request, HttpServletResponse response){
        BufferedImage bufferedImage = new ImageCode().createImage();
        try {
            OutputStream outputStream = response.getOutputStream();
            ImageIO.write(bufferedImage,"jpeg",outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/on")
//    @ResponseBody
    public String on(SysUser sysUser){
        log.error("账号："+sysUser.getAccount()+",密码："+sysUser.getPassword());

//        return "账号："+sysUser.getAccount()+",密码："+sysUser.getPassword();
        return "/info";
    }

    @GetMapping("/info")
    public String info(){

        return "/info";
    }
    @GetMapping("/success")
    @ResponseBody
    public ModelAndView success(ModelAndView modelAndView,String code){
        log.info("code:"+code);
        modelAndView.addObject("user",sysUserService.getById(1));
        modelAndView.setViewName("/success");
        return modelAndView;
    }




}
